import axios from 'axios';
import qs from 'qs';
import { VALID_LOGIN } from '_config/url'
import context from '../main.js'
import router from '../router'

axios.defaults.timeout = 20000;
axios.defaults.baseURL ='';
axios.defaults.withCredentials=true;


//http request 拦截器
// axios.interceptors.request.use(
//   config => {
//     // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
//     config.data = JSON.stringify(config.data);
//     // config.data = qs.stringify(config.data)
//     config.headers = {
//       "Content-Type":"application/json;charset=utf-8",
//     // }
//     // if(token){
//     //   config.params = {'token':token}
//     }
//     return config;
//   },
//   error => {
//     return Promise.reject(err);
//   }
// );


//http response 拦截器
// axios.interceptors.response.use(
//   response => {
//     if(response.data.errCode ==2){
//       router.push({
//         path:"/login",
//         querry:{redirect:router.currentRoute.fullPath}//从哪个页面跳转
//       })
//     }
//     return response;
//   },
//   error => {
//     return Promise.reject(error)
//   }
// )


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url,params={}){
  return new Promise((resolve,reject) => {
    axios.get(url,{
      params:params
    },{
      headers:{
        "Content-Type":"application/json;charset=utf-8"
      }
    })
    .then(response => {
      resolve(response.data);
    })
    .catch(err => {
      reject(err)
    })
  })
}

//退出到首页
function selectPlate(){
  let a = document.createElement('a')
  a.href = location.protocol + '//'+location.host
  a.click()
}
/**
 * 封装postsso请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function postsso(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.post(url,
     qs.stringify(data),{
      headers:{
        "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
      }
     }
         )
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

 export function post(url,data = {}){
   return new Promise((resolve,reject) => {
    axios.post(url,JSON.stringify(data),{
      headers:{
        "Content-Type":"application/json;charset=utf-8"
      }
     } )
          .then(response => {
            resolve(response.data);
          },err => {
            reject(err)
          })
   })
 }

 /**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.patch(url,data)
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}

 /**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.put(url,data)
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}